﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;
using OpenGado.Business;
namespace OpenGado.Win
{
    public partial class frmVendorManager : Form
    {
        public frmVendorManager()
        {
            InitializeComponent();
        }

     

        private void VendorForm_Load(object sender, EventArgs e)
        {

            PopulateGrid();
        }

        private void VendorDetails (int vendorId)
        {
            // Fetch vendor object 
            Vendor vendor = VendorDataControl.Instance.GetbyId(vendorId);
           

            // Fetch contacts 
            IList<Contact> contacts = VendorDataControl.Instance.GetAllContacts(vendorId);

            //for each contact Fetch address list 

            // for each contact Fetch phone list

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmVendorDetails vendorDetailsForm = new frmVendorDetails();

            vendorDetailsForm.MdiParent = this.MdiParent;
            vendorDetailsForm.Show();
        }

        private void PopulateGrid()
        {
            dgvVendorList.Rows.Clear();

            dgvVendorList.AutoGenerateColumns = false;

            IList<Vendor> vendors = VendorDataControl.Instance.GetAllVendors();
         
          
            
            foreach (Vendor v in vendors)
            {
                DataGridViewRow rw = new DataGridViewRow();
                int n = dgvVendorList.Rows.Add(rw);
                rw.Tag = v;

                // link the cells
                dgvVendorList.Rows[n].Cells[0].Value = v.VendorId;
                dgvVendorList.Rows[n].Cells[1].Value = v.VendorName;

                if (v.Contact.Count > 0)
                {
                    dgvVendorList.Rows[n].Cells[2].Value = v.Contact[0].ContactName;
                    dgvVendorList.Rows[n].Cells[3].Value = v.Contact[0].ContactPhone; 
                }
               
                dgvVendorList.Rows[n].Cells[4].Value = v.VendorWebsite;
                
            }
           

        }

        private List<Vendor> SelectedVendors()
        {
            List<Vendor> selectedVendors = new List<Vendor>();

            foreach (DataGridViewRow rw in dgvVendorList.SelectedRows)
            {
                selectedVendors.Add((Vendor)rw.Tag);
            }

            return selectedVendors;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult res;

            res = MessageBox.Show("Are you sure you want to delete selected Vendors ? ", "Inventory", MessageBoxButtons.YesNo);

            if (res == DialogResult.No) return;
            
            foreach (Vendor v in SelectedVendors())
            {
                VendorDataControl.Instance.Delete(v);
            }
            PopulateGrid();
        }

        private void dgvVendorList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvVendorList.CurrentRow != null)
            {
                Vendor v = (Vendor) dgvVendorList.CurrentRow.Tag;

                frmVendorDetails vendorDetailsForm = new frmVendorDetails(v.VendorId);
                vendorDetailsForm.UpdateVendorDetails += new frmVendorDetails.VendorDetailsUpdateHandler(PopulateGrid);
                vendorDetailsForm.MdiParent = this.MdiParent;
                vendorDetailsForm.Show();
            }
        }
    }
}
